package servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.Product_Info;
import service.Product_InfoService;
import service.impl.Product_InfoServiceImpl;
import util.PageUtil;


public class ProductInfoServlet extends HttpServlet {
	private Product_InfoService pis=new Product_InfoServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String searchText = request.getParameter("searchText");
		String searchMethod = request.getParameter("searchMethod");
		HashMap<String, String> map = new HashMap<String, String>();

		map.put("searchText", searchText);
		map.put("searchMethod", searchMethod);
		// 获取 前端 传递过来的 pageNo
				String pageNo = request.getParameter("pageNo");
		if (pageNo == null || "".equals(pageNo)) {
			pageNo = "1";
		}
		// 定义默认的 pageSize
		Integer pageSize = 7;
		// 查询出 符合 条件的 总条数
		Integer rows = pis.getRows(map);
		// 合理化 pageNo
		PageUtil pageUtil = new PageUtil(Integer.parseInt(pageNo), pageSize, rows);
		// 分页查询
		 List<Product_Info> allProduct = pis.getAllProduct(pageUtil.getPageNo(), pageUtil.getPageSize(),map);
		// 将 分页好的 数据 传入 pageUtil对象
				
		// 存入请求中
		request.setAttribute("allProduct", allProduct);
		request.setAttribute("pageUtil", pageUtil);
		request.setAttribute("map", map);
		// 转发到 指定 页面
		request.getRequestDispatcher("productManagement.html").forward(request, response);	
		
		
		
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
